package com.siqi.hr.system.common.utils;

import org.apache.commons.lang.StringUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Title: 日期工具<br>
 * Description: <br>
 * Date: 2016-4-12
 * 
 * @author xiaoxm
 * @version 1.0
 */
public final class DateUtil {

    /**
     * default date format pattern
     */
    public final static String DATE_FORMAT = "yyyy-MM-dd";
    public final static String MONTH_DATE_FORMAT = "MM-dd";
    public final static String YEAR_WEEK_FORMAT = "yyyy-ww";
    public final static String YEAR_MONTH_FORMAT = "yyyy-MM";
    public final static String YEAR_WEEK_FORMAT_SHORT = "yy-ww";
    public final static String YEAR_MONTH_FORMAT_SHORT = "yy-MM";
    public final static String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm";
    public final static String FULL_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public final static String TIME_FORMAT = "HH:mm";
    public final static String MONTH_DAY_HOUR_MINUTE_FORMAT = "MM-dd HH:mm";
    public final static String LOCATE_DATE = "yyyyMMdd";
    public final static String LOCATE_DATE_FORMAT = "yyyyMMddHHmmss";
    public final static String LOCATE_DATE_CHINA = "yyyy年MM月dd日";

    private static final int DAYS_OF_A_WEEK = 7;

    private DateUtil() {
    }

    /**
     * 获取增加小时后的 Date
     * @param date
     * @param i
     * @return squall add 20100225
     */
    public static Date addHour(Date date, int i) throws Exception {
        Calendar calendar = getDefaultCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, i);
        return calendar.getTime();
    }

    /**
     * 指定String时间转Date
     * @param date 如：“2016-05-01”
     * @param pattern 如：“yyyy-MM-dd”
     * @return 
     * @throws Exception
     */
    public static String formatDate(Date date, String pattern){
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 计算2个时间相差的天数
     * @param fromDate
     * @param endDate
     * @return
     * @throws Exception
     */
    public static int getDateNum(Date fromDate, Date endDate) throws Exception {
        long days = (endDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24);
        return (int) days;
    }

    /**
     * 时间加天数
     * @param date
     * @param number
     * @return
     * @throws Exception
     */
    public static Date addDate(Date date, int number) throws Exception {
        Calendar calendar = getDefaultCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, number);
        return calendar.getTime();
    }

    /**
     * get the default calendar
     * 
     * @return the calendar instance
     */
    public static Calendar getDefaultCalendar() throws Exception {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        return calendar;
    }

    /**
     * 获取一年中某周,星期几的日期
     * @param yearNum
     * @param weekNum
     * @param dayOfWeek
     * @return
     */
    private static Date getDateOfYearWeek(int yearNum, int weekNum, int dayOfWeek) throws Exception {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
        cal.setMinimalDaysInFirstWeek(DAYS_OF_A_WEEK);
        cal.set(Calendar.YEAR, yearNum);
        cal.set(Calendar.WEEK_OF_YEAR, weekNum);
        return cal.getTime();
    }

    /**
     * 比较两个世界的大小
     * @author             xiaogq
     * @param beginDate    开始时间
     * @param endDate      结束时间
     * @return
     * @throws Exception
     */
    public static boolean before(Date beginDate, Date endDate) throws Exception {
        return beginDate.compareTo(endDate) <= 0;
    }

    /**
     * 获得两个时间的年、月、日、时、分、秒
     * @param beginTime       开始时间字符串
     * @param endTime         结束时间字符串
     * @param formatPattern  格式化字符串
     * @param returnPattern  标识(0:年，1;月，2：日，3：时，4：分，5：秒)
     * @return
     * @throws ParseException
     */
    public static long getBetween(String beginTime, String endTime, String formatPattern, int returnPattern){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatPattern);
        Date beginDate = null,endDate = null;
        try {
            if(StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime)){
                beginDate = simpleDateFormat.parse(beginTime);
                endDate = simpleDateFormat.parse(endTime);
            }else{
                beginDate = new Date();
                endDate = new Date();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar beginCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();
        beginCalendar.setTime(beginDate);
        endCalendar.setTime(endDate);
        switch (returnPattern) {
            case 0:
                return DateUtil.getByField(beginCalendar, endCalendar, Calendar.YEAR);
            case 1:
                return DateUtil.getByField(beginCalendar, endCalendar, Calendar.YEAR)*12 + DateUtil.getByField(beginCalendar, endCalendar, Calendar.MONTH);
            case 2:
                return DateUtil.getTime(beginDate, endDate)/(24*60*60*1000);
            case 3:
                return DateUtil.getTime(beginDate, endDate)/(60*60*1000);
            case 4:
                return DateUtil.getTime(beginDate, endDate)/(60*1000);
            case 5:
                return DateUtil.getTime(beginDate, endDate)/1000;
            default:
                return 0;
        }
    }

    private static long getByField(Calendar beginCalendar, Calendar endCalendar, int calendarField){
        return endCalendar.get(calendarField) - beginCalendar.get(calendarField);
    }

    private static long getTime(Date beginDate, Date endDate){
        return endDate.getTime() - beginDate.getTime();
    }

    public static void main(String[] args) throws Exception {
    	System.out.println();
	}
}
